package com.tanhua.server.service;

import com.tanhua.domain.db.Dimensions;
import com.tanhua.domain.db.Questions;
import com.tanhua.domain.db.Report;
import com.tanhua.domain.vo.*;
import com.tanhua.dubbo.api.SoulApi;
import com.tanhua.server.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SoulService {
    @Reference
    private SoulApi soulApi;

    public List<SoulQuestionnaireVo> getTestSoul() {
        Long loginUsrId = UserHolder.getUserId();
        List<SoulQuestionnaireVo> vos = soulApi.findQustion(loginUsrId);
        System.out.println(vos);
        return vos;
    }

    public String postTestSoul(AnswersVo answers) {
        List<Questions> questionsList = soulApi.saveQuestion(answers);

        System.out.println(questionsList);
        Integer sum = 0;
        for (Questions questions : questionsList) {
            sum += questions.getScore();
        }
        String reportId = "0";
        if (sum < 28) {
            reportId = "1";
        } else if (sum >= 28 && sum < 35) {
            reportId = "2";
        } else if (sum >= 35 && sum < 40) {
            reportId = "3";
        } else {
            reportId = "4";
        }
        Dimensions dimensions = new Dimensions();
        int outgoing = 0;
        int outgoingMax = 0;
        int judgment = 0;
        int judgmentMax = 0;
        int abstracts = 0;
        int abstractsMax = 0;
        int reason = 0;
        int reasonMax = 0;
        int index = 0;
        for (Questions questions : questionsList) {
            index++;
            if (index < 3) {
                outgoing += questions.getScore();
                outgoingMax += questions.getMaxScore();
            } else if (index > 3 && index < 6) {
                judgment += questions.getScore();
                judgmentMax += questions.getMaxScore();
            } else if (index > 6 && index < 8) {
                abstracts += questions.getScore();
                abstractsMax += questions.getMaxScore();
            } else {
                reason += questions.getScore();
                reasonMax += questions.getMaxScore();
            }

        }

        dimensions.setOutgoing(Math.floor(((float) outgoing / (float) outgoingMax) * 100));
        dimensions.setJudgment(Math.floor(((float) judgment / (float) judgmentMax) * 100));
        dimensions.setAbstracts(Math.floor(((float) abstracts / (float) abstractsMax) * 100));
        dimensions.setReason(Math.floor(((float) reason / (float) reasonMax) * 100));
        dimensions.setUserId(UserHolder.getUserId().intValue());
        soulApi.updateDimensions(dimensions);
        soulApi.updateQuestion(answers, reportId,UserHolder.getUserId());
        return reportId;
    }

    public ReportVo getDimensions(String reportId) {
        ReportVo reportVo = new ReportVo();
        List<Dimensions> dimensionsList= soulApi.findDimentsions();
       Report report= soulApi.findReport(reportId);
        Map<Integer, Dimensions> dimensionsMap = dimensionsList.stream().collect(Collectors.toMap(Dimensions::getUserId, u -> u));
        Dimensions dimensions = dimensionsMap.get(UserHolder.getUserId().intValue());
        List<DimensionsVo> dimensionsVoList = new ArrayList<>();
        DimensionsVo dimensionsVo = new DimensionsVo();
        dimensionsVo.setKey("外向");
        dimensionsVo.setValue(String.valueOf(dimensions.getOutgoing()));
        dimensionsVoList.add(dimensionsVo);
        DimensionsVo dimensionsVo1 = new DimensionsVo();
        dimensionsVo1.setKey("判断");
        dimensionsVo1.setValue(String.valueOf(dimensions.getJudgment()));
        dimensionsVoList.add(dimensionsVo1);
        DimensionsVo dimensionsVo2 = new DimensionsVo();
        dimensionsVo2.setKey("抽象");
        dimensionsVo2.setValue(String.valueOf(dimensions.getAbstracts()));
        dimensionsVoList.add(dimensionsVo2);
        DimensionsVo dimensionsVo3 = new DimensionsVo();
        dimensionsVo3.setKey("理性");
        dimensionsVo3.setValue(String.valueOf(dimensions.getReason()));
        dimensionsVoList.add(dimensionsVo3);
        reportVo.setDimensions(dimensionsVoList);
        reportVo.setConclusion(report.getConclusion());
        reportVo.setCover(report.getCover());
        for (Dimensions recommendUser : dimensionsList) {
          //  recommendUser.getAbstracts()-dimensions.getAbstracts()
        }



        List<SimilarYou> similarYouList = new ArrayList<>();
        SimilarYou similarYou = new SimilarYou();
        similarYou.setAvatar("https://sztanhua.oss-cn-shenzhen.aliyuncs.com/avatar/man/4.jpg");
        similarYou.setId(1);
        similarYouList.add(similarYou);
        SimilarYou similarYou1 = new SimilarYou();
        similarYou1.setAvatar("https://sztanhua.oss-cn-shenzhen.aliyuncs.com/avatar/man/4.jpg");
        similarYou1.setId(2);
        similarYouList.add(similarYou1);
        SimilarYou similarYou2 = new SimilarYou();
        similarYou2.setAvatar("https://sztanhua.oss-cn-shenzhen.aliyuncs.com/avatar/man/4.jpg");
        similarYou2.setId(3);
        similarYouList.add(similarYou2);
        reportVo.setSimilarYou(similarYouList);
        return reportVo;
    }
}
